//
//  GCMSLRViewController.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/3/23.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "GCMSLRViewController.h"
#import "GCMSLRTableViewCell.h"
#import "GCMSLRModel.h"

@interface GCMSLRViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>

@end

@implementation GCMSLRViewController{

    UITextField *_textF ;
    UILabel *_label;
    UIButton *_searchBtn;
    UIButton *_bmBtn;
    UITableView *_tableV;
    NSMutableArray *_array ;
    NSMutableArray *_bmArray;
    NSMutableArray *_bmIDArray;
    NSMutableArray *_dataSource;
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self downLoadData];
    [self createTableView];
    
}


-(void)createTableView{
    
    _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0,190 - 64, SCREEN_WIDTH, SCREEN_HEIGHT  - 190 +64)];
    _tableV.backgroundColor  = [UIColor redColor];
    _tableV.delegate = self;
    _tableV.dataSource =self;
    _tableV.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.0];
    [self.view addSubview:_tableV];
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GCMSLRTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[GCMSLRTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [cell setBackgroundColor:[UIColor colorWithRed:.93 green:.93 blue:.93 alpha:1]];
    //    cell.textLabel.textColor = [UIColor colorWithRed:0.39 green:0.39 blue:0.39 alpha:1];
    GCMSLRModel *listModel = _dataSource[indexPath.row];
    //NSLog(@"%@",listModel.contact);
    [cell setSlrModel:listModel];
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 41;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *str = [[NSString alloc]init];
    str = [_dataSource[indexPath.row] valueForKey:@"name"];
    //NSLog(@"%@",str);
    //2.回传数据
    self.returnStrBlock(str);
    
    if ([self.type isEqualToString:@"1"]) {
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"是否要选择？" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
    }else{
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        
        if (buttonIndex == 1) {
            [self.navigationController popViewControllerAnimated:YES];
        }
}

-(void)initData{

    _array = [[NSMutableArray alloc]init];
    _bmArray = [[NSMutableArray alloc]init];
    _bmIDArray = [[NSMutableArray alloc]init];
}

-(void)downLoadData{

    [GCMReviceHttpRequst postFenLeiDataWithURL:TASKPOSITION_URL Success:^(id obj, NSError *err) {
        _array = obj;
        NSLog(@"%@",_array);
        [self createUI];
    }];
    
    [GCMReviceHttpRequst postReciveListDataWithUserName:[GCMUserInfo instance].realName WithSearchName:@"" WithPositionId:@"" WithPage:@"1" Success:^(id obj, NSError *err) {
        _dataSource = obj;
        [_tableV reloadData];
    }];
    
}

-(void)createUI{
    self.view.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.00];
    
    _textF = [[UITextField alloc]initWithFrame:CGRectMake(20, 74, SCREEN_WIDTH - 100, 40)];
    _textF.placeholder = @"请输入查找的用户名";
    [self.view addSubview:_textF];
    
    _searchBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 80, 74, 60, 35)];
    _searchBtn.backgroundColor = [UIColor colorWithRed:0.00 green:0.67 blue:0.65 alpha:1.00];
    [_searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
    [_searchBtn addTarget:self action:@selector(onSearchButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_searchBtn];
    
    _label = [[UILabel alloc]initWithFrame:CGRectMake(20, 110, SCREEN_WIDTH - 110, 2)];
    _label.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:_label];
    
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGColorRef color = CGColorCreate(colorSpaceRef, (CGFloat[]){1.0,1,1,1});
    
    _bmBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 120, SCREEN_WIDTH, 50)];
    _bmBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -50, 0, 0);//设置title在button上的位置（上top，左left，下bottom，右right）
    [_bmBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_bmBtn setBackgroundColor:[UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1]];
    UIImageView *rimageViwe = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 70, 0, 50, 50)];
    [rimageViwe setImage:[UIImage imageNamed:@"xiala"]];
    [_bmBtn addSubview:rimageViwe];
    [_bmBtn.layer setBorderColor:color];
    [_bmBtn.layer setBorderWidth:1.f];
    [_bmBtn.layer setCornerRadius:0];
    [_bmBtn addTarget:self action:@selector(onbmButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_bmBtn];
    //_yearArr = [NSMutableArray arrayWithObjects:[NSString stringWithFormat:@"%d",[dastr intValue]-1],dastr, nil];
    NSMutableArray *fenleiA = [[NSMutableArray alloc]init];
    NSMutableArray *fenleiB = [[NSMutableArray alloc]init];
    //NSLog(@"%@",_bmArray);
    [fenleiA addObject:@"全部部门"];
    [fenleiB addObject:@""];
    for (NSDictionary *dic in _array) {
        NSString *str =[ NSString stringWithFormat:@"%@", [dic valueForKey:@"name"] ];
        NSString *str1 =[ NSString stringWithFormat:@"%@", [dic valueForKey:@"id"] ];
        [fenleiA addObject:str];
        [fenleiB addObject:str1];
        _bmArray = [NSMutableArray arrayWithArray:fenleiA];
        _bmIDArray  = [NSMutableArray arrayWithArray:fenleiB];
    }
    //NSLog(@"%@",_bmArray);
    [_bmBtn setTitle:_bmArray[0] forState:UIControlStateNormal];
}


#pragma mark--展示下拉菜单
-(void)onbmButton{
    
    __weak typeof(self)weakSelf=self;
    if (!_mune) {
        
        _mune=[[SCXCreateAlertView alloc]initWithNameArray:_bmArray andMenuOrigin:CGPointMake(50,170 ) andMenuWidth:SCREEN_WIDTH - 100 andHeight:90 andLayer:0 andTableViewBackGroundColor:[UIColor clearColor] andIsSharp:YES andType:kNavigationPop];
        _mune.delegate = weakSelf;
        _mune.tableViewDelegate=self;
        _mune.dismiss=^(){
            [weakSelf.mune removeFromSuperview];
            _mune=nil;
        };
        [self.view addSubview:_mune];
    }
    else{
        [_mune dismissWithCompletion:^(SCXCreateAlertView *create){
            [_mune removeFromSuperview];
            _mune = nil;
        }];
    }
}

#pragma mark--自定义下拉菜单代理方法,点击下拉菜单某一行时候对应的响应时间，点击完成后试图要消失，用一个block通知；
-(void)tableViewDidSelectRowAtIndexPath:(NSIndexPath *)indexPath andPopType:(popType)popViewType{
    
    //改变导航栏文字
    if (popViewType == kNavigationPop) {
        [_bmBtn setTitle:_bmArray[indexPath.row] forState:UIControlStateNormal];
    }
//    NSLog(@"%@",_bmArray[indexPath.row]);
    [_mune dismissWithCompletion:nil];//点击完成后视图消失;
    
    [GCMReviceHttpRequst postReciveListDataWithUserName:[GCMUserInfo instance].realName WithSearchName:_textF.text WithPositionId:_bmIDArray[indexPath.row] WithPage:@"1" Success:^(id obj, NSError *err) {
        _dataSource = obj;
        [_tableV reloadData];
    }];
    
    
}

//查询用户
- (void)onSearchButtonClick{
    
    NSString *str  = [[NSString alloc]init];
    for (NSArray *a in _array) {
        if ([_bmBtn.titleLabel.text isEqualToString:[a valueForKey:@"name"]]) {
            str  = [a valueForKey:@"id"];
        }else if([_bmBtn.titleLabel.text isEqualToString:@"全部部门"]){
           str = @"";
        }
    }
    
    [GCMReviceHttpRequst postReciveListDataWithUserName:[GCMUserInfo instance].realName WithSearchName:_textF.text WithPositionId:str WithPage:@"1" Success:^(id obj, NSError *err) {
        _dataSource = obj;
        [_tableV reloadData];
    }];
}

//收键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [_textF resignFirstResponder];
}
//点击return收键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
